package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.MyAppAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.AppBean;
import com.shenzhen.horselive.beans.MyAppBean;
import com.shenzhen.horselive.databinding.ActivityMyAppBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

import java.util.ArrayList;
import java.util.List;

public class MyAppActivity extends AppBarActivity {

    ActivityMyAppBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMyAppBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleDark();
        setTitle("我的应用");
        setMenuText("添加");
        initViews();
        initData();
    }





    private void initData() {
        ApiFactory.getApiService().myAppList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<MyAppBean>(this) {
                    @Override
                    protected void onNextInActive(MyAppBean myAppBean) {
                        if (myAppBean.isSuccess())
                        {
                            appAdapter.setNewData(myAppBean.getData());

                        }else
                        {
                            showToast(myAppBean.getErrMessage());
                        }
                    }
                });


    }
    MyAppAdapter  appAdapter;
    private void initViews() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
          appAdapter = new MyAppAdapter(R.layout.item_my_app);
          binding.recyclerview.setAdapter(appAdapter);
          appAdapter.setEmptyView(R.layout.no_data_layout);
         appAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (getIntent().getBooleanExtra("iscall",false))
                {
                Intent  intent= new Intent();
                intent.putExtra("data",appAdapter.getItem(position));
                setResult(1001,intent);
                finish();
                }
//                else
//                {
//                    //编辑app
//                    AppBean   appBean =  appAdapter.getItem(position);
//                    Intent  intent = new Intent(MyAppActivity.this,AddAppActivity.class);
//                    intent.putExtra("data",appBean);
//                    startActivityForResult(intent,1000);
//                }
            }
        });

        //编辑按钮
        appAdapter.addChildClickViewIds(R.id.img_bianji);
        appAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId())
                {
                    case R.id.img_bianji:
                        //编辑按钮
                        //编辑app
                        AppBean   appBean =  appAdapter.getItem(position);
                        Intent  intent = new Intent(MyAppActivity.this,AddAppActivity.class);
                        intent.putExtra("data",appBean);
                        startActivityForResult(intent,1000);
                        break;
                }
            }
        });
    }

    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        Intent  intent = new Intent(this,AddAppActivity.class);
        startActivityForResult(intent,1000);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1000&&resultCode==1001)
        {
            //添加地址刷新
            initData();
        }
    }
}